<?php

namespace App\Console\Scripts\Counter;

use App\Common\Libs\ArrayHelper;
use App\Console\BaseCommand;
use App\Models\Cache\Cache;

/**
 * 计数统计脚本
 * [!] 计数基类 不执行具体逻辑
 * Class ArticleCounterCommand
 * @package App\Console\Scripts\Counter
 */
class BaseCounterCommand extends BaseCommand
{

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'counter:base {action}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'counter for base, run `php artisan counter:base init` to init all counter ';

    /**
     * keys sort
     *
     * @var string
     */
    protected $sort = '[1/1]';

    /**
     * 初始化所有模块计数
     * @return int
     */
    public function init()
    {
        // 脚本过多，防止内存不够
        ini_set('memory_limit','12800M');
        $classList = $this->getClassList();
        foreach ($classList as $key => $class) {
            $this->logger(PHP_EOL . "start $class ..." . PHP_EOL, true, 'script');
            $this->call($class, ['action' => 'all']);
            $this->logger(PHP_EOL . "end $class ." . PHP_EOL, true, 'script');
        }
        return 1;
    }

    /**
     * 刷新模块下所有计数
     * @return int
     */
    public function all()
    {
        $methods = $this->getMethods();
        foreach ($methods as $key => $method) {
            try {
                $this->sort = "[".($key + 1)."/".count($methods)."]";
                $this->logger(PHP_EOL . "start $method ..." . PHP_EOL, true, 'script');
                $this->$method();
                $this->logger(PHP_EOL . "end $method ." . PHP_EOL, true, 'script');
            } catch (\Exception $e) {
                $msg = "[ERR] method : $method - {$e->getMessage()} @file: {$e->getFile()} : {$e->getLine()} .";
                $this->error($msg);
                continue;
            }
        }
        return 1;
    }

    /**
     * list modules
     * @return int
     */
    protected function modules()
    {
        $classList = $this->getClassList();
        $this->info(' modules list : ');
        foreach ($classList as $key => $class) {
            $this->info("   ".($key + 1).". $class");
        }
        return 1;
    }

    /**
     * get class list
     * @param string $dir
     * @param string $namespace
     * @return array
     */
    protected function getClassList($dir = __DIR__, $namespace = __NAMESPACE__)
    {
        return parent::getClassList($dir, $namespace);
    }

    /**
     * get self methods
     * @param string $parent
     * @return array
     */
    protected function getMethods($parent = __CLASS__)
    {
        return parent::getMethods($parent);
    }

    /***
     * set hash cache
     * @param $params
     * @param $cacheKey
     * @param $data
     * @param bool $isForce
     * @return string
     */
    protected function setHashCache($params, $cacheKey, $data, $isForce = false)
    {
        $cache = Cache::getInstance($params);
        $count = array_sum(array_values($data));
        // 计数大于 0 才存储
        if ($count || $isForce) {
            // 脚本执行产生的缓存，一年后过期
            $cache->hset($cacheKey, ArrayHelper::merge($cache->hGetAll($cacheKey), $data), 365 * 86400);
        }
        return $cacheKey = $cache->getKey($cacheKey, true);
    }

}
